package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.ISystemDictionaryService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    //查询所有
    @RequestMapping("/list")
    @RequiresPermissions(value = {"systemDictionary:list", "字典目录列表"},logical = Logical.OR)
    public String list(Model model,@ModelAttribute("qo")QueryObject qo){

        PageInfo pageInfo = systemDictionaryService.query(qo);
        model.addAttribute("result",pageInfo);
        return "/systemDictionary/list";
    }

    //删除
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"systemDictionary:delete", "字典目录删除"},logical = Logical.OR)
    public JsonResult delete(Long id){
        systemDictionaryService.deleteByPrimaryKey(id);
        return new JsonResult();
    }

/*    @RequiredPermission(name = "字典目录编辑页面")
    @RequestMapping("/input")
    @ResponseBody
    public JsonResult input(Long id, Model model){
        try {
            if (id != null) {
                SystemDictionary systemDictionary = systemDictionaryService.get(id);
                model.addAttribute("systemDictionary",systemDictionary);
            }
        }catch (Exception e){
            return new JsonResult(false,"保存失败,请重新处理!");
        }
        return null;
    }*/

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"systemDictionary:saveOrupdate", "字典目录新增/编辑"},logical = Logical.OR)
    public JsonResult saveOrupdate(SystemDictionary systemDictionary){
        try {
            if (systemDictionary == null) {
                systemDictionaryService.save(systemDictionary);
            }else{
                systemDictionaryService.updateByPrimaryKey(systemDictionary);
            }
            return new JsonResult();
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"操作失败,请重新操作!!");
        }
    }
}
